package com.example.demo;

/**
 * 找到最接近 0 的数字
 * https://leetcode.cn/problems/find-closest-number-to-zero/description/
 */
public class FindClosestNumber {

    public int findClosestNumber(int[] nums) {
        int div = Math.abs(nums[0]);
        int res = nums[0];

        for (int n : nums) {
            if (Math.abs(n) < div) {
                div = Math.abs(n);
                res = n;
            } else if (Math.abs(n) == div) {
                res = Math.max(res, n);
            }
        }

        return res;
    }
}
